package servlet.admin;

import entity.Borrow_books;
import entity.Borrow_card;
import entity.Pages;
import com.alibaba.fastjson.JSONObject;
import service.admin.CardListService;
import service.admin.impl.CardListServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * CardListServlet
 *
 * @author lenovo
 * @since 2023/9/4
 */
@WebServlet("/admin/cardList")
public class CardListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    //引入服务层
    CardListService service = new CardListServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        // 接收参数
        String limit = req.getParameter("limit");
        String page = req.getParameter("page");
        String condition = (String) req.getParameter("condition");
        String conditionValue = (String) req.getParameter("conditionValue");
        PrintWriter out = resp.getWriter();
        if (page == null) {
            page = "1";
        }
        if (limit == null) {
            limit = "10";
        }

        Pages<Borrow_card> pages = new Pages<>();
        pages.setPageSize(Integer.valueOf(limit));
        pages.setPageCountNo(Integer.valueOf(page));
        int totalCount = service.totalCount(condition,conditionValue);
        pages.setPageCount(totalCount);
        List<Borrow_card> list = service.getList(condition,conditionValue,pages);
        pages.setList(list);

        int code = 1;
        String msg = "error";
        int count = 0;

        if (list!= null){
            code = 0;
            msg = "成功";
            count = pages.getPageCount();
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("count",count);
        jsonObject.put("data",pages.getList());
        out.print(jsonObject.toJSONString());
        out.flush();
        out.close();
    }
}
